package com.itheima.wallet.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author HuangYuChen
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = getFieldValByName("create_time", metaObject);
        if (null == created) {
            //创建时间字段为空可以进行填充
            setFieldValByName("create_time", new Date(), metaObject);
        }
        Object updated = getFieldValByName("update_time", metaObject);
        if (null == updated) {
            //更新时间字段为空，可以自动填充
            setFieldValByName("update_time", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时更新数据库中“更新时间”字段
        setFieldValByName("update_time", new Date(), metaObject);
    }
}
